package com.bluemartini.jmx.mbean;

import com.bluemartini.client.BusinessActionClient;
import com.bluemartini.core.ValueAccumulator;
import com.bluemartini.dna.BMException;
import com.bluemartini.dna.BMLog;
import com.bluemartini.dna.DNAList;
import com.bluemartini.dna.DNAListParser;
import com.bluemartini.server.BMClient;

/**
 * Expose Business action as MBean for management
 * 
 * @author Yannick Robin
 *
 */

public class BusinessAction implements BusinessActionMBean {

	private String name;
	private static DNAList dnaStats_;
	private static long lastCheckTime;
	private final static int BIZACT_STATS_CHECK_SECONDS = 5*1000; //5 seconds
	
	public BusinessAction(String name)
	{
		this.name = name;
	}

	/**
	 * Execute business action
	 * TODO Set optional context (pubversion, default database, user...)
	 * 
	 * @param sInput A String that can be deserialized to a DNAList
	 */
	public String execute(String sInput) {
		if (sInput == null)
			return "[PARAMETER MISSING]";
		
		DNAListParser parser = null;
        parser = new DNAListParser(sInput);
        try {
			parser.parse();
		} catch (BMException e) {
			e.printStackTrace();
			return "[PARSING ERROR] " + e.getMessage();
		}
        DNAList dnaInput = parser.getList();
        
		DNAList dnaOutput;
		try {
			dnaOutput = BMClient.executeBusinessAction(name, dnaInput);
		} catch (BMException e) {
			e.printStackTrace();
			return "[EXECUTION ERROR] " + e.getMessage();
		}
		
		if (dnaOutput == null)
			return "";
		else
			return dnaOutput.toString();
			
	}
	
	public long getCount()
	{
		ValueAccumulator va = getStatAccumulator();
		if (va == null)
			return 0;
		else
			return va.getCounter();
	}
	
	public double getAverageExecutionTime()
	{
		ValueAccumulator va = getStatAccumulator();
		if (va == null)
			return 0;
		else
			return va.getAverage();
	}
	
	public double getMaxExecutionTime()
	{
		ValueAccumulator va = getStatAccumulator();
		if (va == null)
			return 0;
		else
			return va.getMax();
	}
	
	public double getMinExecutionTime()
	{
		ValueAccumulator va = getStatAccumulator();
		if (va == null)
			return 0;
		else
			return va.getMin();
	}
	
	private ValueAccumulator getStatAccumulator()
	{
		ValueAccumulator va = null;
		DNAList dnaStats = getStats();
		if (dnaStats == null)
			return null;
		DNAList bizactList = dnaStats.getList("bizact");
		if (bizactList == null)
			return null;
		DNAList dnaBizact = bizactList.getList(name);
		if (dnaBizact == null)
			return null;
		va = (ValueAccumulator)dnaBizact.getObject("elapsed");
		return va;
	}
	
	private synchronized DNAList getStats()
	{
		long currentTime = System.currentTimeMillis();
		if (currentTime - lastCheckTime > BIZACT_STATS_CHECK_SECONDS)
		{
			dnaStats_ = BusinessActionClient.getStats();
			lastCheckTime = currentTime;
			BMLog.log(BMLog.COMPONENT_SYSTEM, 2, "BM JMX: Refresh bizact stats");
		}		
		return dnaStats_;
	}
}
